<uni-icons type=""></uni-icons><template>

	<view class="uni-common-mt">
		<view class="logo-box">
			<image src="../../../static/image/loginLogo.png" mode="center" class="login-logo"></image>
		</view>


		<form class="loginForm" @submit="login">
			<view class="uni-form-item uni-column" style="width: 690rpx;">
				<!-- <input class="my_input" id="my_input" name="input" v-model="username" placeholder="请输入账户名" /> -->
				<uni-easyinput class="my_input" id="my_input" type="clearable" name="input" v-model="userName"
					placeholder="请输入账户名" :styles="styles" trim="all" :clearable="true">
				</uni-easyinput>
			</view>
			<view class="uni-form-item uni-column" style="margin-bottom: 70px; width: 690rpx;">
				<!-- <input class="my_input" id="my_input" password name="input" v-model="password" placeholder="请输入密码" /> -->
				<uni-easyinput class="my_input" id="my_input" type="password" name="input" v-model="password"
					placeholder="请输入密码" :styles="styles" trim="all">
				</uni-easyinput>
			</view>
			<!-- <view class="forget-register">
				<text class="forget">忘记密码</text>
				<text>|</text>
				<text class="register">新用户注册</text>
			</view> -->
			<view class="uni-btn-v">
				<button class="sub_button" form-type="submit">登录</button>
			</view>
			<!-- <uni-icons type="cart" size="30"></uni-icons> -->
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userName: '',
				password: '',
				styles: {
					borderColor: '#EBEFF6'
				},
				id: '',
				uniqueCode: '',
				memberName: '',
				memberTypeText: '',
				genderText: '',
			}
		},
		onLoad() {
			let that = this;
			//缓存的账号
			const HC_name = uni.getStorageSync('HC_name');

			//缓存的密码
			const HC_pass = uni.getStorageSync('HC_pass');

			//有缓存就赋值给文本没有就清空
			if (HC_name && HC_pass) {
				that.userName = HC_name;
				that.password = HC_pass;
			} else {
				that.userName = '';
				that.password = '';
			}
		},
		methods: {
			// async getAppConfig() {
			// 	const res = await this
			// 		.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
			// 			url: '/api-mobile/config',
			// 			method: "get",
			// 		})
			// 	uni.setStorageSync('AppConfig', res.body.command)
			// },
			onNavigationBarButtonTap() {
				// console.log("123")

				// 缓存的账号
				const HC_names = uni.getStorageSync('HC_name');
				// console.log(HC_names)
				if (HC_names != '') {
					uni.switchTab({
						url: '/pages/index/index'
					});
				} else {
					uni.showToast({
						title: '请先登录',
						mask: true,
						icon: "none"

					});

				}

				// //缓存的密码
				// const HC_passs = uni.getStorageSync('HC_pass');

			},
			async login() { //async 定义的函数会默认的返回一个Promise对象resolve的值
				// console.log(this.userName)
				const res = await this
					.$myRequest({ //await 关键字 只能放在 async 函数内部， await关键字的作用 就是获取 Promise中返回的内容， 获取的是Promise函数中resolve或者reject的值
						url: '/api-operate/families/app/security/share/login',
						method: "post",
						data: {
							userName: this.userName,
							password: this.password
						},
					})
				// console.log(res)
				// console.log(res.body.currentUser.members)
				// console.log(res.body.currentUser.id)
				if (res.success == true) { //如果登录成功
					// this.getAppConfig();
					// console.log('登录后获取到的全部信息: '+JSON.stringify(res.body));
					uni.setStorageSync('HC_name', this.userName);
					uni.setStorageSync('HC_pass', this.password);
					uni.setStorageSync('loginName', res.body.currentUser.loginName)
					// 用于健康数据模块的专业检测link拼接
					uni.setStorageSync('idToken', res.body.currentUser.idToken);
					uni.setStorageSync('wristbandMac', res.body.currentUser.members[0].wristbandMac)
					console.log('idToken再登录那一边：' + uni.getStorageSync('idToken'));
					console.log('wristbandMac再登录那一边：' + uni.getStorageSync('wristbandMac'));
					uni.setStorageSync('idCard', res.body.currentUser.members[0].idCard);
					uni.setStorageSync('imei', res.body.currentUser.members[0].wristbandImei);
					uni.setStorageSync('membersArray', res.body.currentUser.members);
					// console.log('登录后获取到的： '+JSON.stringify(res.body.currentUser.members))
					// var loginName = uni.getStorageSync('loginName')
					uni.setStorageSync('x-auth-token', res.body.currentUser.idToken); //获取服务端传递过来的token信息
					uni.setStorageSync('name', res.body.currentUser.name)
					uni.setStorageSync('houseNo', res.body.currentUser.members[0].uniqueCode)
					uni.setStorageSync('relationId', res.body.currentUser.id)
					// this.familyList=res.body.currentUser.members
					// console.log(this.familyList)
					let model = res.body.currentUser.members
					var house = []
					let memberNameArray = []
					for (let i = 0; i < model.length; i++) {
						memberNameArray.push({
							memberName: model[i].memberName,
							uniqueCode: model[i].uniqueCode
						})
						if (house == null || house == '') {
							house = res.body.currentUser.members[i].uniqueCode
						} else {
							house = house + ',' + res.body.currentUser.members[i].uniqueCode
						}
						// this.id = model[i].id
						// this.uniqueCode = model[i].uniqueCode
						// this.memberName = model[i].memberName
						// this.memberTypeText = model[i].memberTypeText
						// this.genderText = model[i].genderText
					}
					uni.setStorageSync('memberNameArray', memberNameArray)
					uni.setStorageSync('house', house)
					// console.log(id)

					uni.setStorageSync('account', res.body.currentUser.members[0].account)
					uni.setStorageSync('id', res.body.currentUser.members[0].id)
					uni.setStorageSync('uniqueCode', res.body.currentUser.members[0].uniqueCode)
					uni.setStorageSync('memberName', res.body.currentUser.members[0].memberName)
					uni.setStorageSync('memberTypeText', res.body.currentUser.members[0].memberTypeText)
					uni.setStorageSync('genderText', res.body.currentUser.members[0].genderText)
					uni.setStorageSync('gender', res.body.currentUser.members[0].gender)
					uni.switchTab({
						url: '/pages/user/index'
					});
					try {
						const userModule = uni.requireNativePlugin("LJY-UserModule") //调用android中自定义的插件名称
						//执行涂鸦智能登录,使用账户密码登录
						const ret = userModule.login({ //login表示自定义插件的函数名，第一个表示要传递的参数
							userName: "13123481660", //18077799766,ay1627002092336IE868
							password: "12345678" //1qaz2wsx34
						});
						// console.log(ret)
						uni.hideLoading(); //加载结束
						// uni.switchTab({
						// 	url: '/pages/user/index'
						// });

					} catch (e) { //如果涂鸦智能无法连接
						uni.hideLoading(); //加载结束
						// uni.showModal({
						// 	title: '系统提示',
						// 	content: "涂鸦智能家居暂无法登录，将无法使用涂鸦智能家居功能",
						// 	showCancel: false,
						// 	success: function(res) {
						// 		if (res.confirm) {
						// 			uni.switchTab({
						// 				url: '/pages/user/index'
						// 			});
						// 		}
						// 	}
						// });
					}
				} else {
					uni.showToast({
						title: '登录失败',
						mask: true,
						duration: 2000,
						icon: "none"
					});
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.logo-box {
		width: 100%;
		height: 328rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.login-logo {
		width: 200rpx;
		height: 200rpx;
		background-color: #DA422A;
		border-radius: 50%;
	}

	/* @import url("/static/style/test.css"); */
	.run {
		width: 750rpx; //750rpx,350rpx
	}

	form.loginForm>view {
		margin: 50rpx;
	}

	.my_input {
		height: 76rpx;
		margin: 10px 5px;
		margin: 0 36rpx 10rpx 36rpx;
		position: relative;
	}

	#my_input::after {
		content: "";
		width: 100%;
		height: 2px;
		background: #DBDBDB;
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		margin: 0 auto;
	}

	.forget-register {
		color: #B0B0B0;
		margin: 62rpx 0 84rpx 0;
		padding: 0 36rpx 0 0;
		display: flex;
		justify-content: flex-end;
	}

	.forget-register text:nth-child(2) {
		margin: 0 20rpx 0 20rpx;
	}
</style>
